<template>
  <div class="page-box">
    <!-- 筛选区域 -->
    <div v-show="flag" class="search-box">
      <!-- :inline="true" 表单在一行显示 -->
      <el-form size="mini" :inline="true" class="demo-form-inline">
        <el-form-item label="标签名称">
          <el-input v-model="quearyData.tagname" placeholder="请输入标签名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchTag">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetCate">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="open('add')">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download" size="mini">下载</el-button>
        </div>

        <!-- 添加弹出层 -->
        <el-dialog
          :title="title"
          :visible.sync="dialogVisible"
          width="30%"
          :before-close="handleClose"
        >
          <el-form ref="form" :model="form" label-width="100px" class="demo-ruleForm">
            <el-form-item
              label="标签名称"
              prop="tagname"
              :rules="[
                { required: true, message: '标签名称不能为空',trigger:'blur'},
              ]"
            >
              <el-input v-model="form.tagname" autocomplete="off" placeholder="请输入标签名称`" />
            </el-form-item>
            <el-form-item>
              <el-button @click="resetForm">取消</el-button>
              <el-button type="primary" @click="submit">确定</el-button>
            </el-form-item>
          </el-form>

        </el-dialog>

        <div>
          <el-button v-if="flag" icon="el-icon-arrow-up" size="mini" plain circle @click="flag=!flag" />
          <el-button v-else icon="el-icon-arrow-down" size="mini" plain circle @click="flag=!flag" />
          <el-button icon="el-icon-refresh" size="mini" plain circle @click="initDate()" />
          <el-button icon="el-icon-menu" size="mini" plain circle />
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table
        :data="list"
        border
        style="width: 100%"
      >
        <el-table-column type="index" label="#" width="50" />
        <el-table-column prop="id" label="ID" />
        <el-table-column prop="tagname" label="标签名称" />
        <el-table-column prop="" label="操作">
          <template #default="{row}">
            <el-button circle type="primary" icon="el-icon-edit" size="mini" @click="open('edit',row.id)" />
            <el-button circle type="danger" icon="el-icon-delete" size="mini" @click="del(row.id)" />
          </template>
        </el-table-column>
      </el-table>

    </div>
    <!-- 分页区域 -->
    <div class="page">
      <el-row type="flex" justify="end">
        <el-pagination
          :current-page="quearyData.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="quearyData.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
  </div>
</template>

<script>

import { getTagList, addTagname, editTag, updateTag, delTag } from '@/api/content/tag'
export default {
  data() {
    return {
      flag: true,
      form: {
        tagname: ''
      },
      dialogVisible: false,
      title: '',
      titleType: '',
      list: [],
      quearyData: {
        tagname: '',
        page: 1,
        limit: 10
      },
      // 数据总数
      total: 5,
      id: ''
    }
  },
  created() {
    this.initData()
  },
  methods: {
    del(id) {
      console.log(id)
      this.$confirm('确定删除吗,是否继续?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delTag({ id })
        // console.log(this.list.length)
        if (this.list.length <= 1) {
          this.params.page -= 1
        }
        this.initDate()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加和修改标签
    async submit() {
      if (this.titleType === 'add') {
        this.$refs.form.validate(async isOk => {
          if (!isOk) return
          try {
            await addTagname(this.form)
            this.handleClose()
            this.initData()
          // console.log(res)
          } catch (error) {
            console.log(error)
          }
        })
      }
      if (this.titleType === 'edit') {
        await updateTag({
          default_data: 0,
          id: this.id,
          tagname: this.form.tagname
        })
        this.handleClose()
        this.initData()
      }
    },
    // 关闭弹框
    resetForm() {
      this.handleClose()
    },
    // 控制弹框的标题
    async open(type, id) {
      this.dialogVisible = true
      this.titleType = type
      if (this.titleType === 'add') {
        this.title = '添加标签'
      }
      if (this.titleType === 'edit') {
        this.title = '修改标签'
        // console.log(id)
        this.id = id
        const res = await editTag({ id })
        // console.log(res)
        this.form = res.data
      }
    },
    // 弹框的回调函数
    handleClose() {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    // 更改每一页的条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.quearyData.limit = val
      this.quearyData.page = 1
      this.initData()
    },
    // 更改页数
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.quearyData.page = val
      this.initData()
    },
    // 重置
    resetCate() {
      this.quearyData = {
        catename: '',
        page: 1,
        limit: 10
      }
      this.initData()
    },
    // 搜索
    searchTag() {
      this.quearyData.page = 1
      this.initData()
    },
    // 渲染
    async initData() {
      const res = await getTagList(this.quearyData)
      // console.log(res)
      this.list = res.data.data
      this.total = res.data.count
    }
  }
}
</script>

<style lang="scss" scoped>
.page-box{
  padding: 20px;
  .search-box,.tabbar-box {
      padding-top: 10px;
      border:  1px solid #eee;
      margin-bottom: 20px;
      height: 50px;
    }
}
</style>
